<template lang="jade">
    table(class="table table-bordered")
        thead
            tr
                th(v-for="item in nameList") {{item.name}}
                th(style="width:100px") 价格
                th(style="width:100px") 备注
                th(style="width:100px",v-if="toHistory")
        tbody
            tr(v-for="productSku in productSkuList")
                td(v-for="propertyItem in productSku.propertyItems") {{propertyItem.name}}
                td
                    input(v-model="productSku.price",class="form-control input-sm")
                td
                    input(v-model="productSku.remark",class="form-control input-sm")
                td(style="width:100px",v-if="toHistory")
                    a(:href="'#/warehousing-history/'+productSku.id") 出入库记录
            tr(v-if="productSkuList.length===0")
                td(colspan="100") 暂无数据


</template>
<script>
    import ProductSku from '../models/product-sku';
    import _ from 'lodash';
    export default {
        data(){
            return {
                productSkuList:[]
            }
        },
        computed:{
            /**
             * 属性名（name）和属性id集合
             */
            nameList(){
                if(this.productSkuList.length==0)return [];
                var productSku = this.productSkuList[0];
                var  nameList =  _.map(productSku.propertyItems, (propertyItem)=> {
                    if(!propertyItem.property)return null;
                    return {
                            name:propertyItem.property.name,
                            id:  propertyItem.property.id
                        };
                });
                return _.filter(nameList, (name)=>{return name});
            },
            data(){
                var results = this.row_to_col();
                return results;
            }
        },
        props:{
            productSkuList: {type:Array, default:()=>{return []}},
            toHistory:{type:Boolean, default:false}
        },
        model:{prop:'productSkuList',event:'update'},
        methods:{
            get_product_sku_list(){
                return this.productSkuList;
            },
            update(productSkuList){
                var vm = this;
                // 查找出产品列表中已存在的数据，使价格和备注不改变
                _.each(productSkuList, (productSku)=>{

                    var found = _.find(vm.productSkuList,(existing)=>{
                        return existing.skuId == productSku.skuId;
                    });

                    if(found) {
                        productSku.price=found.price;
                        productSku.remark=found.remark;
                    }
                });
                this.productSkuList=productSkuList;
            }
        },
        mounted(){
            
        }
    }
</script>